package com.Telien.app.ui.main;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import com.Telien.app.request.service.RequestService;

public class LocationPanel extends JPanel implements ActionListener {
	RequestService requestService;
	JButton create_btn;
	JButton file_btn ;
	JLabel lblFile;
	JLabel cords;
	JTextField kmlField;
	JTextArea outputField;
	JScrollPane jScrollPane1;
	private JPanel searchPanel;
	private JPanel buttonPanel;

	public LocationPanel(){
		initialize();
	}
	
	private void initialize() {
		requestService = new RequestService();
		setLayout(new BorderLayout());
	
		create_btn = new JButton("Get Location");
		create_btn.addActionListener(this);
		create_btn.setSize(new Dimension(40, 40));
		file_btn = new JButton("Open KML File");
		file_btn.addActionListener(this);
		lblFile = new JLabel("KML File:");
		cords = new JLabel("Location");
		kmlField = new JTextField(30);
		outputField = new JTextArea();
		outputField.setEditable(false);
		jScrollPane1 = new JScrollPane(outputField);
        //add(outputField,BorderLayout.NORTH);
        //add(kmlField, BorderLayout.CENTER);
		//add(file_btn, BorderLayout.CENTER);
		 searchPanel = new JPanel();

	    FlowLayout experimentLayout = new FlowLayout();
	    searchPanel.setLayout(experimentLayout);
	    searchPanel.add(lblFile);
		searchPanel.add(kmlField);
		searchPanel.add(file_btn);
		add(searchPanel,BorderLayout.NORTH);

		add(jScrollPane1,BorderLayout.CENTER);
	
		add(create_btn,BorderLayout.SOUTH);
		
		/*
		add(lblFile);
		add(kmlField);
		add(cords);
		add(jScrollPane1);
		add(file_btn);
		add(create_btn);
		*/

	}
	
	public void actionPerformed(ActionEvent e) {
		String name = e.getActionCommand();
		if (name.equals("Open KML File")) {
			JFileChooser fileChooser = new JFileChooser();
			fileChooser.showOpenDialog(null);
			File file = fileChooser.getSelectedFile();
			String path = "";
			if (file != null)
				path = file.getPath();
			kmlField.setText(path);
		}
		else if(name.equals("Get Location")){
			outputField.append(kmlField.getText());
			System.out.println('\n'+kmlField.getText());
			if(kmlField.getText()==null){
				outputField.append("Select KML/KMZ File");
			}
			else{
			String location = requestService.getLocation(kmlField.getText());
			outputField.append('\n'+location);
			}
		}
	}
}
